﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.Model;
using MahApps.Metro.Controls;

namespace BTv2
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : MetroWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private async void Login_Click(object sender, RoutedEventArgs e)
        {
            if (usernameField.Text == "" || passwordField.Password == "")
            {
                problemDescriptionLabel.Content = "Username or Password field is empty";
                Storyboard sb = this.FindResource("BTerrorMessageAnimation") as Storyboard;
                Storyboard.SetTargetName(sb, "problemDescriptionLabel");
                sb.Begin();
            }
            else
            {
                bool authentication = await UsersController.GetInstance().ValidateUserCredentials(usernameField.Text,passwordField.Password);
                if (authentication)
                {
                    this.Close();
                    bool isadm = await UsersController.GetInstance().UserAdmin(usernameField.Text);
                    bool userLoginWasRegistered = await UsersController.GetInstance().UserLogged(usernameField.Text);
                    new Settings(isadm).Show();
                }
                else
                {
                    problemDescriptionLabel.Content = "Username or Password are incorrect";
                    Storyboard sb = this.FindResource("BTerrorMessageAnimation") as Storyboard;
                    Storyboard.SetTargetName(sb, "problemDescriptionLabel");
                    sb.Begin();
                }
            }
        }

        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                Login_Click(sender, null);
            }
        }

        private async void Cancel_Click(object sender, RoutedEventArgs e)
        {
        //await Task.Run(()=>DatabaseController.GetInstance().AddUser(new User() { UserName = "Tanev", Password = "asdf", Mail = "g.p.tanev@gmail.com", IsAdmin = true }));
       //     var sensor = new Sensor() { ID = 1 };
       // await    DatabaseController.GetInstance().GetSensorData(sensor, new DateTime(2013, 4, 1), new DateTime(2013, 4, 2));
            
            this.Close();
        }

        private void LoginWindow_Closed(object sender, EventArgs e)
        {
            MainWindow.GetInstance().AttachSettingClick();
        }
    }
}
